<?php

/**
 * Gestionnaire du systeme de Session
 *
 * @category   McCube
 * @package    Session
 * @author     Leiha Sellier <leiha.sellier@gmail.com>
 * @license    http://creativecommons.org/licenses/by/3.0/  CC BY 3.0
 * @link       http://code.google.com/p/mechanics-cubes/
 */
class McCube_Session{

    protected $_datas        = array();
    protected $_notices      = array();
    protected $_noticesForm  = array();
    protected $_database     = null;

    protected static $_instance = null;

    public $menu = array(
         '/Users/'      => 'Utilisateurs'
        ,'/Acl/groups/' => 'ACL'
        ,'/timetrack/'  => 'Pointage'
    );

    public static function getInstanceOf(){
        if(!self::$_instance)
            self::$_instance = new self();

        return self::$_instance;
    }

    public function __construct(){
        session_start();
        $this->_database = McCube_Db::getInstanceOf();
        $this->_request  = new McCube_WebRequest();
        $this->_router   = McCube_Router::getInstanceOf();

        $uri = parse_url($_SERVER['REQUEST_URI']);
        if(!$this->_router->parse($uri['path']))
            throw new McCube_Router_Exception('Route', 404, array(
                'route'=> $this->_uri['path']
            ));
    }

    public function __get($name){
        return $this->get($name);
    }

    public function &get($name){
        return $this->_datas[$name];
    }

    public function set($name, $value){
        if(is_array($value))
            $value = (object)$value;

        $this->_datas[$name] = $value;
    }

    public function destroy(){
         $this->_database->delete()
            ->table   ('sessions')
            ->where   ('session_id ="'.session_id().'"')
            ->limit   (1)
            ->exec()
        ;
    }

    public function restore(){
        $session = $this->_database->select()
            ->table   ('sessions')
            ->fields  ('*')
            ->where   ('session_id ="'.session_id().'"')
            ->limit   (1)
            ->exec()
        ;

        if(!$session = $session->fetch())
            return false;

        $this->_datas = unserialize($session['datas']);
        return true;
    }

    public function save(){
        $session = $this->_database->replace()
            ->table('sessions')
            ->set  (array(
                 'session_id'=> session_id()
                ,'datas'     => serialize($this->_datas)
            ))
            ->exec()
        ;
    }

    public function getRequest(){
        return $this->_request;
    }

    public function getRouter(){
        return $this->_router;
    }

    public function isEmptyNotice(){
        return empty($this->_notices) && empty($this->_noticesForm);
    }

    public function addNotice($type, $msg){
        $this->_notices[$type][] = $msg;
    }

    public function addNoticeForm($type, $formLabel, $formFields=array()){
        $this->_noticesForm[$type][] = array($formLabel, $formFields);
    }

    public function getNotices($type){
        if(isset($this->_notices[$type]))
            return $this->_notices[$type];

        return array();
    }

    public function getNoticesForm($type){
        if(isset($this->_noticesForm[$type]))
            return $this->_noticesForm[$type];

        return array();
    }



}